package inet.app.controller.parser;

import inet.app.controller.connect.JsonParser;
import inet.app.model.object.ListTinh_Sc_Item;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

public class Parser_ListtinhSC {

	private ArrayList<ListTinh_Sc_Item> list = new ArrayList<ListTinh_Sc_Item>();
	private ListTinh_Sc_Item item;
	private String ITEMS = "items";
	private String NAME = "name";
	private String CODE = "code";
	private String ID = "id";

	public Parser_ListtinhSC() {

	}

	public ArrayList<ListTinh_Sc_Item> getList(String url) {
		list = new ArrayList<ListTinh_Sc_Item>();
		JSONObject jobject, object;
		JsonParser jParser = new JsonParser();
		jobject = jParser.getJSONFromUrl(url);
		try {
			JSONArray ja = jobject.getJSONArray(ITEMS);
			int i = 0;
			String code, name;
			for (i = 0; i < ja.length(); i++) {
				object = ja.getJSONObject(i);
				code = object.getString(CODE);
				name = object.getString(NAME);
				item = new ListTinh_Sc_Item(code, name);
				list.add(item);
			}
			return list;
		} catch (Exception e) {
			Log.e("JSON Parser", "Error parsing data " + e.toString());
			return null;
		}

	}

	public ArrayList<ListTinh_Sc_Item> getListTinh(String url) {
		list = new ArrayList<ListTinh_Sc_Item>();
		JSONObject jobject, object;
		try {
			jobject = new JSONObject(url);
			JSONArray ja = jobject.getJSONArray(ITEMS);
			int i = 0;
			String code, name;
			int id;
			for (i = 0; i < ja.length(); i++) {
				object = ja.getJSONObject(i);
				code = object.getString(CODE);
				name = object.getString(NAME);
				id = object.getInt(ID);
				item = new ListTinh_Sc_Item(id, code, name);
				list.add(item);
			}
			return list;
		} catch (Exception e) {
			Log.e("JSON Parser", "Error parsing data " + e.toString());
			return null;
		}

	}

}
